﻿using EquipmentManager.BOLogic;
using EquipmentManager.Entities;
using EquipmentManager.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace EquipmentManager.Controllers
{
    public class LoginController : Controller
    {

        [HttpGet]
        public ActionResult Index()
        {
            /*BOBase boBase = new BOBase();
            boBase.Seed();*/

            return View();
        }

        [HttpPost]
        public ActionResult Index(LoginModel model)
        {
            if (ModelState.IsValid)
            {
                LoginBO loginBO = new LoginBO();
                try 
                {
                    var result = loginBO.Login(model.Login, model.Password);
                    switch (result)
                    {
                        case UserRole.Admin:
                            FormsAuthentication.SetAuthCookie(model.Login, false);
                            return RedirectToAction("Index", "Admin");
                        case UserRole.Operator:
                            FormsAuthentication.SetAuthCookie(model.Login, false);
                            return RedirectToAction("Index", "Operator");
                    }
                }
                catch
                {
                    ModelState.AddModelError("", "Неправильное имя пользователя или пароль!");
                    return View();
                }
            }
            return View();
        }

        /// <summary>
        /// Action for logout.
        /// </summary>
        /// <returns></returns>
        [Authorize]
        public ActionResult Logout()
        {
            FormsAuthentication.SignOut();
            return RedirectToAction("Index", "Login");
        }
    }
}
